/*
 * CurrencyManager.java
 *
 *  
 */
package org.msb.finance.app;

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.Currency;
import java.util.Iterator;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * The {@code CurrencyManager} class manages the list of defined {@link Currency} objects for this application.
 * 
 * The primary responsibilities of this class consist of
 * <ul>
 * <li>maintaining the list of {@link Currency} object that are used by the application</li>
 * <li>maintain a base currency against which all other currency exchange rates are established</li>
 * <li>maintaining the exchange rates between each currency and the base currency</li>
 * </ul>
 * 
 * @author Marc Boudreau
 * 
 */
public class CurrencyManager {

	/*
	 * The base currency is the one against which the exchange rates of all of the currencies are established.
	 */
	private Currency base;

	/*
	 * 
	 */
	private Map<String, Currency> nameToCurrencyMap;

	/*
	 * 
	 */
	private Map<Currency, BigDecimal> currencyExchangeRates;

	/**
	 * 
	 */
	public CurrencyManager() {
		loadCurrencies();
	}

	public void addCurrency(String name, Currency currency) {

	}

	public void removeCurrency(Currency currency) {

	}

	public String[] getCurrencyNames() {
		return null;
	}

	public Currency getCurrency(String currencyName) {
		return null;
	}

	public BigDecimal getExchangeRate(String currencyName) {
		return null;
	}

	public void setBaseCurrency(Currency currency) throws IllegalArgumentException {

	}

	public void renameCurrency(String oldName, String newName) {

	}

	private void loadCurrencies() {
		InputStream is = findCurrenciesFile();
		SAXReader reader = new SAXReader(true);
		Document doc = null;

		try {
			doc = reader.read(is);
		} catch (DocumentException ex) {
			//
		}

		parseDocument(doc);
	}

	private void parseDocument(Document doc) {
		if (null != doc) {
			Element root = doc.getRootElement();
			parseCurrencies(root);
		}
	}

	/*
	 * 
	 */
	@SuppressWarnings("unchecked")
	private void parseCurrencies(Element currenciesElem) {
		for (Iterator<Element> it = currenciesElem.elementIterator("currency"); it.hasNext();) { //$NON-NLS-1$
			parseCurrency(it.next());
		}
	}

	/*
	 * 
	 */
	private void parseCurrency(Element currencyElem) {
		String code = currencyElem.attributeValue("code"); //$NON-NLS-1$
		String location = currencyElem.attributeValue("location"); //$NON-NLS-1$
		String name = currencyElem.attributeValue("name"); //$NON-NLS-1$
	}

	private InputStream findCurrenciesFile() {
		return null;
	}
}
